var mainPicDel = '';
var otherPicDel = '';
/*----------------------上传宝贝主图--------------------*/
function mainChange(input){
	var deleteImgSrc = $('#deleteImgSrc').val();
	var files = input.files[0];
	var img = document.createElement("img");
	var imgDelete = document.createElement("img");
	img.file = files;
	img.className = 'mainImg';
	imgDelete.className = 'deleteImg';
	imgDelete.src = deleteImgSrc;
	if($(input).attr('index') === '000'){
		var str = '<div class="mainImgWrap"><div class="blackWrap">宝贝首图</div></div>';
	}else{
		var str = '<div class="mainImgWrap"><div class="blackWrap">宝贝主图</div></div>';
	}
	
	$(input).parent().parent().append(str);
	$(input).parent().next().get(0).append(img);
	$(input).parent().next().get(0).append(imgDelete);
    var reader = new FileReader();
    reader.onload = (function(aImg) {
      return function(e) {
        aImg.src = e.target.result;
      };
    })(img);
    reader.readAsDataURL(files);
    //删除宝贝主图
	mui('body').on('tap','.mainImgWrap .deleteImg',function(){
		$(this).parent().remove();
	})
	/*//添加图片上传按钮
	if(mui('.bannerWrap').find('.fileWrap').length < 4){
		addBtn('.bannerWrap','mainChange','mainPics','宝贝主图');
	}*/
}
function addBtn(obj,event,name,info){
	var directionStr = '<div class="fileWrap"><div class="upPicture"><div class="blackWrap">'+info+'</div>';
	directionStr +='<input type="file" name="'+name+'" accept="image/gif,image/jpeg,image/jpg,image/png,image/svg" onchange="'+event+'(this)"/>';
	directionStr +='<img src="/static/images/weishang/12@2x.png" alt="" /></div>';
	$(obj).append(directionStr);
}
//删除宝贝主图
mui('body').on('tap','.mainImgWrap .deleteImg',function(){
	if($(this).attr('mainPic') != null && $(this).attr('mainPic') != ''){
		mainPicDel +=$(this).attr('mainPic')+',';
	}
	var len = $('.bannerWrap').find('.fileWrap').length;
	$(this).parent().remove();
	/*addBtn('.bannerWrap','mainChange','mainPics');*/
})

/*----------------------宝贝描述-----------------------*/
function directionChange(input){
	var deleteImgSrc = $('#deleteImgSrc').val();
	var files = input.files[0];
	var img = document.createElement("img");
	var imgDelete = document.createElement("img");
	img.file = files;
	img.className = 'detailImg';
	imgDelete.className = 'deleteImg';
	imgDelete.src = deleteImgSrc;
	var str = '<div class="detailImgWrap"><div class="blackWrap">宝贝描述</div></div>';
	$(input).parent().parent().append(str);
	$(input).parent().next().get(0).append(img);
	$(input).parent().next().get(0).append(imgDelete);
    var reader = new FileReader();
    reader.onload = (function(aImg) {
      return function(e) {
        aImg.src = e.target.result;
      };
    })(img);
    reader.readAsDataURL(files);
    //添加图片上传按钮
    addBtn('.directionImgWrap','directionChange','otherPics','宝贝描述');
    //删除宝贝描述
	mui('body').on('tap','.detailImgWrap .deleteImg',function(){
		$(this).parent().parent().remove();
	})
}

mui('body').on('tap','.detailImgWrap .deleteImg',function(){
	console.log($(this).attr('otherPic'));
	if($(this).attr('otherPic') != null && $(this).attr('otherPic') != '' && $(this).attr('otherPic') != 'undefined'){
		otherPicDel +=$(this).attr('otherPic')+',';
	}
	console.log(otherPicDel);
	var len = $('.directionImgWrap').find('.fileWrap').length;
	$(this).parent().parent().remove();
})

/*------------------------下拉列表-------------------------*/

$('.secondSelect').find('b').html($('.secondCategory').find('li.checked').html());
//初始化下拉列表
(function initSelect(){
	if($('.firstCategory').find('li.checked').html()){
		$('.firstSelect').find('b').html($('.firstCategory').find('li.checked').html());
	}else{
		$('.firstSelect').find('b').html('一级类目');
	}
	if($('.secondCategory').find('li.checked').html()){
		$('.secondSelect').find('b').html($('.secondCategory').find('li.checked').html());
	}else{
		$('.secondSelect').find('b').html('二级类目');
	}
})();
mui('body').on('tap','.firstSelect',function(){
	if($('.secondSelect').attr('status') == 'false'){
		$('.secondCategory').hide();
		$('.secondSelect').attr('status','true');
	}
	if($(this).attr('status') == 'true'){
		$('.firstCategory').show();   
		$(this).attr('status','false');
	}else{
		$('.firstCategory').hide();   
		$(this).attr('status','true');
	}
})

//选择一级类目后，传递二级类目
mui('body').on('tap','.firstCategory li',function(){
	$(this).addClass('checked').siblings().removeClass('checked');
	$('.firstSelect').find('b').html($(this).html());
	$(this).parent().hide();
	$('.firstSelect').attr('status','true');
	var mallCategoryId = $(this).attr("mallCategoryId");
	$.ajax({
		type:"POST",
		url : "/weishang/item/secMallCategoryJson.json",
		dataType : "json",
		data : {
			"mallCategoryId" :mallCategoryId,
		},
		success : function(data) {
			if(data["responseEntity"]["code"] == "200") {
				$('.secondCategory').html('');
				$('.secondSelect').find('b').html('二级目录');
				for(var i = 0;i < data["responseEntity"]["data"].length; i++){
					var newNode = document.createElement("li"); 
					var itemCategoryId = data['responseEntity']['data'][i].id;
					newNode.setAttribute('itemCategoryId', itemCategoryId);
					newNode.innerHTML = data["responseEntity"]["data"][i].name; 
					$('.secondCategory').append(newNode); 
					mui('body').on('tap','.secondCategory li',function(){
						$(this).addClass('checked').siblings().removeClass('checked');
						$('.secondSelect').find('b').html($(this).html());
						$(this).parent().hide();
						$('#mallcategoryid').val($(this).attr('itemCategoryId'));
						$('.secondSelect').attr('status','true');
					})
				}
			}
		}
	})
})
mui('body').on('tap','.secondSelect',function(){
	if($('.firstSelect').attr('status') == 'false'){
		$('.firstCategory').hide();
		$('.firstSelect').attr('status','true');
	}
	if($(this).attr('status') == 'true'){
		$('.secondCategory').show();   
		$(this).attr('status','false');
	}else{
		$('.secondCategory').hide();   
		$(this).attr('status','true');
	}
})

/*------------------------价格设置/分销商设置--------------------------*/
var pricePageStatus = false,dealerPageStatus = false;
mui('body').on('tap','.seePriceWrap',function(){
	//显示价格设置
	$('.mainContent').hide();
	$('.child1').show();
	$('.saveIcon').hide();
	$('.backIcon').hide();
})

mui('body').on('tap','.dealerHref',function(){
	//显示分销商设置
	$('.child1').hide();
	$('.child2').show();
	$('#dealerId').val($(this).attr('id'));
	showInfo($(this).attr('id'));
})

function showInfo(id){
	var info = $('#dealerInfo'+id).val();
	if(Boolean(info)){
		$('#dealerPrice').val(info.split(',')[0]);
		$('#dealerNum').val(info.split(',')[1]);
		var status = info.split(',')[2];
		if(!status){
			status = 0;
		}
		$('#dealerStatus').val(status);
		if(info.split(',')[2] == 0 && !$('.dealerStatus').is('.mui-active')){
			$('.dealerStatus').addClass('mui-active');
		}else if(info.split(',')[2] == 1){
			$('.dealerStatus').removeClass('mui-active');
		}
	}
	$('#dealerInfo'+id).parent().find('.nextPageInfo').html(info);
}

/*--------------------------确定按钮----------------------------*/
//判断分销商价格是否全部设置完成
function judgeInfo(){
	var len = $('#list').find('.dealerHref').length;
	for(var i = 1;i <= len;i ++){
		if(!Boolean($('#dealerInfo'+i+'').val().split(',')[0])){
			return false;
		}
	}
	return true;
}
mui('body').on('tap','.child2Btn',function(){
	//确定，价格设置页面显示
	var dealerPrice = $('#dealerPrice').val();//价格
	var dealerNum = $('#dealerNum').val();//起订量
	var status = $('#dealerStatus').val();
	var dealerId = $('#dealerId').val();
	if(Boolean(dealerPrice) && Boolean(dealerNum) && Boolean(status)){
		setPrice(dealerId,dealerPrice,dealerNum,status);
		$('.child1').show();
		$('.child2').hide();
		$('#dealerPrice').val('');
		$('#dealerNum').val('');
		dealerPageStatus = true;
	}else{
		//提示信息未填写完整
		tip('信息填写不完整');
	}
})
//将分销商设置的价格写入隐藏域
function setPrice(dealerId,dealerPrice,dealerNum,status){
	$('#dealerInfo'+dealerId).val(dealerPrice+","+dealerNum+","+status);
	$('#dealerInfo'+dealerId).parent().find('.nextPageInfo').html(dealerPrice+","+dealerNum+","+status);
}
mui('body').on('tap','.priceSubmit',function(){
	//确定，主页面显示
	/*alert(Boolean(price)+"\n"+judgeInfo());*/
	judgeInfo();
	var price = $('#priceSet').val();
	if(Boolean(price) && judgeInfo()){
		//信息填写完整，可确定
		$('.mainContent').show();
		$('.child1').hide();
		$('.saveIcon').show();
		$('.backIcon').show();
		pricePageStatus = true;
	}else{
		//提示信息未填写完整
		tip('信息填写不完整');
	}
})
/*------------------------状态更新--------------------------*/
function judge(obj,status){
	if($(obj).is('.mui-active')){
		$(status).val(0);
	}else{
		$(status).val(1);
	}
}
/*是否上架*/
judge('.upStatus','#upStatus');
mui('body').on('tap','.upStatus',function(){
	judge('.upStatus','#upStatus');
})

/*是否允许订货*/
judge('.dealerStatus','#dealerStatus');
mui('body').on('tap','.dealerStatus',function(){
	judge('.dealerStatus','#dealerStatus');
})


//友情提示弹框
function tip(error){
	$('#errorInfo').html(error);
	$('.levelPriceTip').show();
	$('#errorTip').show();
}
//友情提示确认
mui('body').on('tap','.queren',function(){
	$('.levelPriceTip').hide();
	$('#errorTip').hide();
})

/*-----------------------保存------------------------------*/
var saveStatus = false;
function imgJudge(){
	if(($('.mainImgWrap').length > 0) && ($('.detailImgWrap').length > 0)){
		saveStatus = true;
	}
	return saveStatus;
}
mui('body').on('tap','.saveIcon',function(){
	var itemCategoryId = $('#mallcategoryid').val();
	$('#checkedLi').val(itemCategoryId);
	var numSet = $('#numSet').val();
	var titleName = $('#titleName').val();
	var description = $('#description').val();
	var test = $('#checkedLi').val();
	var aMain = mainPicDel.split(',');
	aMain.pop();
	var aOther = otherPicDel.split(',');
	aOther.pop();
	$('#mainPicDel').val(aMain.join(','));
	$('#otherPicDel').val(aOther.join(','));
	if(dealerPageStatus && pricePageStatus){
		if(Boolean(itemCategoryId) && Boolean(numSet) && Boolean(titleName) && Boolean(description) && imgJudge()){
			//信息填写完整，可提交
			$('form').submit();
		}else{
			//信息填写不完整
			tip('信息填写不完整');
		}
	}else{
		tip('请重新确认价格！');
	}
	
})
